package com.zues.samples.dao.Impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.type.StringType;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Repository;

import com.zues.samples.dao.BaseHibernateDAO;
import com.zues.samples.dao.UserDAO;
import com.zues.samples.model.PagerModel;
import com.zues.samples.model.User;
import com.zues.samples.service.modelVO.UserVO;
import com.zues.samples.util.SystemContext;

@Repository
public class UserDAOImpl extends BaseHibernateDAO<User> implements UserDAO {

	/**
	 * 列出所有用户
	 * 
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<User> findAllUsers() throws Exception {
		String hql = "FROM User";
		List<User> users = this.createQuery(hql).list();
		return users;
	}

	/**
	 * 创建一个用户
	 * 
	 * @param user
	 * @throws Exception
	 */
	@Override
	public void createUser(User user) throws Exception {
		this.save(user);
	}

	/**
	 * 通过UserId获取User
	 * 
	 * @param id
	 * @return
	 */
	@Override
	public User getUserById(String UserId) throws Exception {
		User user = new User();
		user = this.get(UserId);
		return user;
	}

	/**
	 * 编辑用户
	 * 
	 * @param user
	 * @throws Exception
	 */
	@Override
	public void editUser(User user) throws Exception {
		User exsitsUser = this.get(user.getUserID());
		if (exsitsUser != null) {
			this.update(user);
		}
	}

	/**
	 * 删除用户
	 */
	@Override
	public void deleteUser(int UserId) throws Exception {
		User exsitsUser = this.get(UserId);
		if (exsitsUser != null) {
			this.delete(exsitsUser);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public PagerModel<UserVO> findAllByPage() {
		PagerModel<UserVO> page = new PagerModel<UserVO>();
		ArrayList<UserVO> dates = new ArrayList<UserVO>();

		int offset = SystemContext.getOffset();
		int pageSize = SystemContext.getSize();

		List<User> tempDates = this.createQuery("FROM User").setFirstResult(offset).setMaxResults(pageSize).list();
		for (User user : tempDates) {
			UserVO uservo = new UserVO();
			BeanUtils.copyProperties(user, uservo);
			dates.add(uservo);
		}
		page.setDates(dates);
		page.setOffset(offset);
		page.setPageSize(pageSize);

		// 获取总记录数
		long totalSize = (long) this.createQuery("select count(*) from User").uniqueResult();
		page.setTotalSize(totalSize);

		return page;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<String> getAllUserBySubject(Integer subjectId) {
		StringBuilder sql = new StringBuilder();
		sql.append(" select user.user_id userId ");
		sql.append(" from exam_user_subject eus ");
		sql.append(" left join exam_subject es on eus.subject_id = es.sub_id ");
		sql.append(" left join exam_user user on user.user_id = eus.user_id ");
		sql.append(" where es.sub_id = :subjectId ");

		List<String> temp = this.createSqlQuery(String.valueOf(sql)).addScalar("userId", StringType.INSTANCE)
				.setInteger("subjectId", subjectId).list();
		
		return temp;
	}

	@Override
	public void updateUserLoginFailCount(User user) throws Exception {
		StringBuilder sql = new StringBuilder();
		sql.append(" update exam_user set ");
		sql.append(" login_fail_count = :Count ");
		sql.append(" where user_id =  :Id ");
		
		this.createSqlQuery(String.valueOf(sql)).setInteger("Count", user.getLoginFailCount())
		.setString("Id", user.getUserID()).executeUpdate();
		
	}

	@Override
	public void updateUserLastLockTime(User user) throws Exception {
		StringBuilder sql = new StringBuilder();
		sql.append(" update exam_user set ");
		sql.append(" last_lock_time = :Time ");
		sql.append(" where user_id =  :Id ");
		
		this.createSqlQuery(String.valueOf(sql)).setTimestamp("Time", user.getLastLockTime())
		.setString("Id", user.getUserID()).executeUpdate();
		
	}

}
